package view;

import java.text.SimpleDateFormat;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.sql.Date;
import controller.ContactControllerListener;
import po.Room;
import po.Student;
import service.ContactService;

public class ContactDialog extends JDialog{
	private JPanel panel;
	private ContactControllerListener contactControllerListener;
	private JTextField text1,text2,text3,text4,text5,text7,text8;
	private ContactService service;
	private String[] listData = new String[]{"121室", "122室", "123室", "124室"};
	private JComboBox<String> comboBox = new JComboBox<String>(listData);
	private ContactFrame contactFrame;
	public ContactDialog(ContactFrame owner,int way,Student student) {
		if(way==1) {
			this.contactFrame = owner;
			this.contactControllerListener=owner.getController();
			panel = new JPanel();
			Box vBox = Box.createVerticalBox();
			Box hBox = Box.createHorizontalBox();
			JLabel label1 = new JLabel("姓名");
			text1 = new JTextField();
			JLabel label2 = new JLabel("学号");
			text2 = new JTextField();
			JLabel label3 = new JLabel("生日");
			text3 = new JTextField();
			JLabel label4 = new JLabel("电话");
			text4 = new JTextField();
			JLabel label5 = new JLabel("邮箱");
			text5 = new JTextField();
			JLabel label6 = new JLabel("房间");
			JLabel label7 = new JLabel("籍贯");
			text7 = new JTextField();
			JLabel label8 = new JLabel("家庭地址");
			text8 = new JTextField();
			vBox.add(label1);
			vBox.add(text1);
			vBox.add(label2);
			vBox.add(text2);
			vBox.add(label3);
			vBox.add(text3);
			vBox.add(label4);
			vBox.add(text4);
			vBox.add(label5);
			vBox.add(text5);
			vBox.add(label6);
			vBox.add(comboBox);
			vBox.add(label7);
			vBox.add(text7);
			vBox.add(label8);
			vBox.add(text8);
			JButton ensure = new JButton("确认");
			ensure.setActionCommand("ensure1");
			ensure.addActionListener(contactControllerListener);
			hBox.add(ensure);
			vBox.add(hBox);
			panel.add(vBox);
			setContentPane(panel);
			setVisible(true);
			setBounds(600, 400, 450, 450);
		}
		else if(way==2){
			this.contactFrame = owner;
			this.contactControllerListener=owner.getController();
			panel = new JPanel();
			Box vBox = Box.createVerticalBox();
			Box hBox = Box.createHorizontalBox();
			JLabel label1 = new JLabel("姓名");
			text1 = new JTextField(student.getStudentName());
			JLabel label2 = new JLabel("学号");
			text2 = new JTextField(student.getStudentNo().toString());
			JLabel label3 = new JLabel("生日");
			text3 = new JTextField(student.getBirthday().toString());
			JLabel label4 = new JLabel("电话");
			text4 = new JTextField(student.getTel());
			JLabel label5 = new JLabel("邮箱");
			text5 = new JTextField(student.getEmail());
			JLabel label6 = new JLabel("房间");
			comboBox.setSelectedIndex(student.getRoom().getRoomNo()-1);
			JLabel label7 = new JLabel("籍贯");
			text7 = new JTextField(student.getNPlace());
			JLabel label8 = new JLabel("家庭地址");
			text8 = new JTextField(student.getAddress());
			vBox.add(label1);
			vBox.add(text1);
			vBox.add(label2);
			vBox.add(text2);
			vBox.add(label3);
			vBox.add(text3);
			vBox.add(label4);
			vBox.add(text4);
			vBox.add(label5);
			vBox.add(text5);
			vBox.add(label6);
			vBox.add(comboBox);
			vBox.add(label7);
			vBox.add(text7);
			vBox.add(label8);
			vBox.add(text8);
			JButton ensure = new JButton("确认");
			ensure.setActionCommand("ensure2");
			ensure.addActionListener(contactControllerListener);
			hBox.add(ensure);
			vBox.add(hBox);
			panel.add(vBox);
			setContentPane(panel);
			setVisible(true);
			setBounds(600, 400, 450, 450);
		}else {}
	}		
	public Student getInstance() {
		Student student = new Student();
		service = new ContactService();
		student.setStudentName(text1.getText().trim());
		student.setStudentNo(Long.parseLong(text2.getText().trim()));
		student.setBirthday(strToDate(text3.getText().trim()));
		student.setTel(text4.getText().trim());
		student.setEmail(text5.getText().trim());
		student.setRoom(service.get(comboBox.getSelectedItem().toString().trim()));
		student.setNPlace(text7.getText().trim());
		student.setAddress(text8.getText().trim());
		return student;
	}
	public static java.sql.Date strToDate(String strDate) {
        String str = strDate;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date d = null;
        try {
            d = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date date = new java.sql.Date(d.getTime());
        return date;
    }
	public void refresh(Long studentNo) {
		contactFrame.refresh();
		contactFrame.getSplit().setRightComponent(contactFrame.getPanel(studentNo));
	}
	public JTextField getText2() {
		return text2;
	}
	
}
